/*
 * Review.java
 * Author: Sanjeev Kamboj
 */
package superConference;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Review class - Creates a Review object.
 * 
 * @author Sanjeev Kamboj
 * @updated 11/22/2013
 */
public class Review {

	private static final String REVIEWS_DATA_PATH = "data/reviews.ini";

	/**
	 * The id of a review, only used internally.
	 */
	private int my_id;
	
	/**
	 * An int that tells what paper the review is linked to.
	 */
	private int my_paper_id;
	
	/**
	 * An int that tells what user this paper is associated with
	 */
	private int my_user_id;

	
	/**
	 * A boolean flag for if the review has been submitted.
	 */
	private boolean is_submitted;
	
	/**
	 * The file name (path) for this review.
	 */
	private String my_file_name;
	
	/**
	 * Constructs a Review object.
	 * 
	 * @param user_id The user who is responsible for this review.
	 * @param paper_id The paper_id of the paper whose review this is for.
	 * @param file_name  A string that gives the path of this review.
	 */
	private Review(final int id, final int user_id, final int paper_id, 
				   final String file_name, boolean submitted) {
		my_id = id;
		my_paper_id = paper_id;
		my_user_id = user_id;
		my_file_name = file_name;
		is_submitted = submitted;
	}

	
	/**
	 * Constructs a Review object.
	 * 
	 * @param user_id The user who is responsible for this review.
	 * @param paper_id The paper_id of the paper whose review this is for.
	 * @param file_name  A string that gives the path of this review.
	 */
	public Review(final int user_id, final int paper_id, final String file_name) {
		this(nextID(), user_id, paper_id, file_name, false);
		updateMySelf();
	}
	
	/**
	 * Getter method for the paper_id
	 * @return An int for the paper id that this review is for.
	 */
	public int getPaperId() {
		return my_paper_id;
	}		
	
	/**
	 * Getter method for the user id
	 * @return An int for the user id that this review is for.
	 */
	public int getUserId() {
		return my_user_id;
	}
	
	/**
	 * Sets the submission status to true for the review.
	 */
	public void setSubmission() {
		is_submitted = true;
		updateMySelf();
	}
	
	/**
	 * Getter method for if the review is submitted.
	 * 
	 * @return A boolean for if the review is submitted
	 */
	public boolean isSubmitted() {
		return is_submitted;
	}
	
	/**
	 * A getter method that returns the file path
	 * 
	 * @return A String for where the review is located.
	 */
	public String getFileName() {
		return my_file_name;
	}
	protected void setFileName(final String the_name) {
		my_file_name = the_name;
		updateMySelf();
	}
	
	/**
	 * Returns a string of the paper id it reviews
	 */
	public String toString() {
		return "" + my_paper_id;
		
	}
	
	public String getReview() throws FileNotFoundException {
		final Scanner scanner = new Scanner(new File(my_file_name));
		final StringBuilder sb = new StringBuilder();

		while (scanner.hasNextLine()) {
			sb.append(scanner.nextLine() + "\n");
		}
		scanner.close();
		
		return sb.toString();
	}
	
	/**
	 * The id of a review.
	 * @return the id of a review
	 */
	public int getID() {
		return my_id;
	}
	
	/**
	 * Get a review object base on its id.
	 * @param id the id of the review
	 * @return the Paper object
	 */
	public static Review getReviewByID(int id) {
		File reviews = new File(REVIEWS_DATA_PATH);
		Scanner in;
		Review p = null;
		try {
			in = new Scanner(reviews);
			while(in.hasNextLine()) {
				String line = in.nextLine();
				String[] params = line.split(",");
				if(params.length>1 && Integer.parseInt(params[0]) == id) {
					p = loadFromString(line);
				}
			}
			in.close();
		} catch (FileNotFoundException e) {
			System.err.println(e.getMessage());
		}
		return p;
	}
	
	/**
	 * Get a list of all reviews.
	 * @return a list of all reviews
	 */
	public static List<Review> getAllReviews() {
		List<Review> l = new ArrayList<Review>();
		try {
			Scanner in = new Scanner(new File(REVIEWS_DATA_PATH));
			String line = "";
			while(in.hasNextLine()) {
				line = in.nextLine();
				String[] params = line.split(",");
				if (params.length > 1) {
					l.add(loadFromString(line));
				}
			}
			in.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return l;
	}
	
	/**
	 * Get the next available ID for the paper.
	 * @return the next available ID, or -1 if something unexpected happened
	 */
	private static int nextID() {
		int id = -1;
		try {
			Scanner in = new Scanner(new File(REVIEWS_DATA_PATH));
			String line = "";
			while(in.hasNext()) {
				line = in.nextLine();
			}
			// Should be at the last line
			id = Integer.parseInt(line);
			in.close();
		} catch (FileNotFoundException e) {
			e.getMessage();
		}
		return id;
	}
	
	/**
	 * Generate the string for saving in a text file.
	 * @return the string for saving in a text file
	 */
	private String saveToString() {
		StringBuilder sb = new StringBuilder();
		sb.append(my_id);
		sb.append(",");
		sb.append(my_paper_id);
		sb.append(",");
		sb.append(my_user_id);
		sb.append(",");
		sb.append(my_file_name);
		sb.append(",");
		sb.append(is_submitted);
		return sb.toString();
	}
	
	/**
	 * Generate a Paper from its string representation.
	 * @param s the string representation of a paper
	 * @return the paper
	 */
	private static Review loadFromString(String s) {
		String[] params = s.split(",");
		int id = Integer.parseInt(params[0]);
		int paper_id = Integer.parseInt(params[1]);
		int user_id = Integer.parseInt(params[2]);
		String file_name = params[3];
		boolean submitted = Boolean.parseBoolean(params[4]);
		
		Review r = new Review(id, user_id, paper_id, file_name, submitted);
		return r;
	}
	
	/**
	 * Update the line of this review in reviews.ini.
	 */
	private void updateMySelf() {
		File reviews = new File(REVIEWS_DATA_PATH);
		StringBuilder new_review_text = new StringBuilder();
		Scanner in;
		String line = "";
		try {
			in = new Scanner(reviews);
			while(in.hasNextLine()) {
				line = in.nextLine();
				int review_id = Integer.parseInt(line.split(",")[0]);
				if (review_id == my_id) {
					new_review_text.append(saveToString());
					new_review_text.append("\n");
				} else if (review_id != nextID()){
					new_review_text.append(line);
					new_review_text.append("\n");
				}
				
			}
			in.close();
			int nextID = Integer.parseInt(line.split(",")[0]);
			if (my_id==nextID) {
				nextID++;
			}
			new_review_text.append(nextID);
			new_review_text.append("\n");
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}

		PrintWriter out;
		try {
			out = new PrintWriter(reviews);
			out.write(new_review_text.toString());
			out.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.getMessage();
		}
	}

	
}
